package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AttachFile;
import com.ruoyi.system.mapper.AttachFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CardSetMapper;
import com.ruoyi.system.domain.CardSet;
import com.ruoyi.system.service.ICardSetService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 卡片分类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-20
 */
@Service
public class CardSetServiceImpl implements ICardSetService 
{
    @Resource
    private CardSetMapper cardSetMapper;
    @Resource
    private AttachFileMapper attachFileMapper;

    /**
     * 查询卡片分类管理
     * 
     * @param id 卡片分类管理主键
     * @return 卡片分类管理
     */
    @Override
    public CardSet selectCardSetById(Long id)
    {
        AttachFile attachFile = new AttachFile();
        attachFile.setSourceId(id);
        List<AttachFile> attachFiles = attachFileMapper.selectAttachFileList(attachFile);
        CardSet cardSet = cardSetMapper.selectCardSetById(id);
        cardSet.setAttachFiles(attachFiles.size() != 0 ? attachFiles.get(0) : null);
        return cardSet;
    }

    /**
     * 查询卡片分类管理列表
     * 
     * @param cardSet 卡片分类管理
     * @return 卡片分类管理
     */
    @Override
    public List<CardSet> selectCardSetList(CardSet cardSet)
    {
        return cardSetMapper.selectCardSetList(cardSet);
    }

    /**
     * 新增卡片分类管理
     * 
     * @param cardSet 卡片分类管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCardSet(CardSet cardSet)
    {
        cardSet.setCreateTime(DateUtils.getNowDate());
        int i = cardSetMapper.insertCardSet(cardSet);
        // 卡片只有一个主图 已在前端控制
        if (cardSet.getAttachFiles() != null) {
            cardSet.getAttachFiles().setSourceId(cardSet.getId());
            cardSet.getAttachFiles().setCreateTime(DateUtils.getNowDate());
            attachFileMapper.insertAttachFile(cardSet.getAttachFiles());
        }
        return i;
    }

    /**
     * 修改卡片分类管理
     * 
     * @param cardSet 卡片分类管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCardSet(CardSet cardSet)
    {
        cardSet.setUpdateTime(DateUtils.getNowDate());

        if (cardSet.getAttachFiles() != null){
            if (cardSet.getAttachFiles().getId() != null){
                cardSet.getAttachFiles().setUpdateTime(DateUtils.getNowDate());
                attachFileMapper.updateAttachFile(cardSet.getAttachFiles());
            }else {
                cardSet.getAttachFiles().setSourceId(cardSet.getId());
                cardSet.getAttachFiles().setCreateTime(DateUtils.getNowDate());
                attachFileMapper.insertAttachFile(cardSet.getAttachFiles());
            }
        }

        if (cardSet.getDelAttachFilesIds().length != 0){
            attachFileMapper.deleteAttachFileByIds(cardSet.getDelAttachFilesIds());
        }

        return cardSetMapper.updateCardSet(cardSet);
    }

    /**
     * 批量删除卡片分类管理
     * 
     * @param ids 需要删除的卡片分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCardSetByIds(Long[] ids)
    {
        return cardSetMapper.deleteCardSetByIds(ids);
    }

    /**
     * 删除卡片分类管理信息
     * 
     * @param id 卡片分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCardSetById(Long id)
    {
        return cardSetMapper.deleteCardSetById(id);
    }
}
